package com.yunshang.shoppingmall.common;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class layuiJson {

    private String msg;//信息
    private String msgCode;//信息状态码
    private Object data;//需要返回的数据对象
    private long count;//
    private String errorDetail;//错误信息

    public layuiJson() {
    }

    public static String layuiJsonList(Object object, int count){
        String stringLayuiJsonList;
        if(count>0) {
            stringLayuiJsonList = JSON.toJSONString(object);
            stringLayuiJsonList = "{\"code\":0,\"msg\":\"读取成功\",\"count\":" + count + ",\"data\":" + stringLayuiJsonList + "}";
        }
        else
        {
            stringLayuiJsonList = "{\"code\":1001,\"msg\":\"数据为空\",\"count\":" + count + ",\"data\":[]}";
        }
        return stringLayuiJsonList;
    }

    /**
     * 读取数据并且去掉par中的选项
     * @param object
     * @param count
     * @param par
     * @return
     */
    public static String layuiJsonList(Object object, int count, List<String> par){
        String stringLayuiJsonList="";
        if(count>0) {

            stringLayuiJsonList = JSON.toJSONString(object);
            if(count==1)
            {
                stringLayuiJsonList = "[" + stringLayuiJsonList + "]";
            }
            stringLayuiJsonList = "{\"code\":0,\"msg\":\"读取成功\",\"count\":" + count + ",\"data\":" + stringLayuiJsonList + "}";
            JSONObject jso=JSON.parseObject(stringLayuiJsonList);//json字符串转换成jsonobject对象
            JSONArray jsarr=jso.getJSONArray("data");//jsonobject对象取得some对应的jsonarray数组
            JSONObject ao=jsarr.getJSONObject(0);//jsonarray对象通过getjsonobjext(index)方法取得数组里面的jsonobject对象

            if(par!=null) {
                for (int i = 0; i < par.size(); i++) {
                    System.out.println("要删除的项" + par.get(i));
                    ao.remove(par.get(i));
                }
            }

            System.out.println("删除pwd后的jso：\n"+jso+"\n");

            stringLayuiJsonList = jso+"";
        }
        else
        {
            stringLayuiJsonList = "{\"code\":1001,\"msg\":\"数据为空\",\"count\":" + count + ",\"data\":[]}";
        }
        return stringLayuiJsonList;
    }

    /**
     * 古曲数据，值需要par的选项
     * @param object
     * @param count
     * @param par
     * @return
     */
    public static String layuiJsonList(Object object, int count, String par){
        String stringLayuiJsonList="";
        if (count > 0) {

                stringLayuiJsonList = JSON.toJSONString(object);
                if (count == 1) {
                    stringLayuiJsonList = "[" + stringLayuiJsonList + "]";
                }
                stringLayuiJsonList = "{\"code\":0,\"msg\":\"读取成功\",\"count\":" + count + ",\"data\":" + stringLayuiJsonList + "}";
                JSONObject jso = JSON.parseObject(stringLayuiJsonList);//json字符串转换成jsonobject对象
                JSONArray jsarr = jso.getJSONArray("data");//jsonobject对象取得some对应的jsonarray数组
                JSONObject ao = jsarr.getJSONObject(0);//jsonarray对象通过getjsonobjext(index)方法取得数组里面的jsonobject对象

                System.out.println("删除pwd后的jso：\n" + jso + "\n");

                stringLayuiJsonList = ao.getString(par);
            } else {
                stringLayuiJsonList = "";
            }
        return stringLayuiJsonList;
    }


}
